package com.ice.body.dict.controller;
import com.ice.body.dict.model.SupplierModel;
import com.ice.body.dict.service.SupplierService;
import io.swagger.annotations.ApiOperation;
import com.ice.base.controller.BaseController;
import com.github.pagehelper.PageHelper;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.ice.base.result.ResponseResult;
import com.ice.base.result.AjaxResult;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
/**
 * Created with GuoYanBin.
 * Description:
 * Date: 2021-06-30
 * Time: 09:48
 */
@RestController
@ApiIgnore(value = "字典-供应商")
public class SupplierController extends BaseController{
    //---------------------------字典-供应商-----------------------start
    @Resource
    private SupplierService supplierService;
    
    @ApiOperation(value = "字典-供应商-新增")
    @PostMapping("/api_p/Supplier/insert")
    public AjaxResult<SupplierModel> insert_Supplier(SupplierModel model) {
        AjaxResult r = ResponseResult.success();
        try{

            supplierService.insert(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "字典-供应商-删除")
    @PostMapping("/api_p/Supplier/deleteById")
    public AjaxResult<SupplierModel> deleteById_Supplier(@RequestParam("id") String id) {
        AjaxResult r = ResponseResult.success();
        try{
            Integer count = supplierService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "字典-供应商-更新")
    @PostMapping("/api_p/Supplier/updateById")
    public AjaxResult<SupplierModel> update_Supplier(SupplierModel model) {
        AjaxResult r = ResponseResult.success();
        try{
            Integer count = supplierService.update(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "字典-供应商-查询一个")
    @PostMapping("/api_p/Supplier/selectById")
    public AjaxResult<SupplierModel> selectById_Supplier(@RequestParam("id") String id) {
        AjaxResult r = ResponseResult.success();
        try{
           SupplierModel model = supplierService.selectById(id);
           r.setData(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "字典-供应商-查询多个")
    @PostMapping("/api_p/Supplier/selectAll")
    public AjaxResult<SupplierModel> selectList_Supplier(SupplierModel model) {
        AjaxResult r = ResponseResult.success();
        try{
           List<SupplierModel> list = supplierService.selectListByModel(model);
           r.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "字典-供应商-查询分页")
    @PostMapping("/api_p/Supplier/selectListByPageHelper")
    public AjaxResult<SupplierModel> selectListByPageHelper_Supplier(@RequestParam(name = "pageSize") Integer pageSize,
                                                                     @RequestParam(name = "pageNum") Integer pageNum,
                                                                     SupplierModel model,
                                                                     String keyWord){
        AjaxResult r = ResponseResult.success();
        ArrayList list;
        try{
           PageHelper.startPage(pageNum, pageSize);
           if(StringUtils.isEmpty(keyWord)) {
               list = supplierService.selectListByModel(model);
           } else {
               list = supplierService.selectListByKeyWord(keyWord);
           }
           getReturnResultByPage(r,list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }
    //---------------------------字典-供应商-----------------------end
}
